{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# Agents as Tools with Strands Agents (亚马逊云科技中国区)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "source": [
    "\"Agents as Tools\"（工具型代理）是 AI 系统中的一种多Agnet架构模式，它将专门的 AI agents 包装成可调用的函数（工具），供其他 agents 使用。这创建了一个层次结构：\n",
    "\n",
    "1. 一个主要的\"orchestrator\"（编排器）agent 处理用户交互并确定调用哪个专门的 agent\n",
    "\n",
    "2. 专门的\"tool agents\"在被 orchestrator 调用时执行特定领域的任务\n",
    "\n",
    "这种方法模仿了人类团队动态，其中管理者协调专家，每个专家都带来独特的专业知识来解决复杂问题。与单个 agent 尝试处理所有事情不同，任务被委派给最合适的专业 agent。\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "vscode": {
     "languageId": "plaintext"
    }
   },
   "source": [
    "## 主要优势和核心原则"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "vscode": {
     "languageId": "plaintext"
    }
   },
   "source": [
    "\"Agents as Tools\"模式提供了几个优势：\n",
    "\n",
    "- 关注点分离：每个 agent 都有一个专注的责任领域，使系统更容易理解和维护\n",
    "- 层次化委派：orchestrator 决定调用哪个专家，创建了清晰的命令链\n",
    "- 模块化架构：专家可以独立添加、移除或修改，而不影响整个系统\n",
    "- 提升性能：每个 agent 可以拥有为其特定任务优化的定制系统提示和工具"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: strands-agents in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (0.2.1)\n",
      "Requirement already satisfied: boto3<2.0.0,>=1.26.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents) (1.38.38)\n",
      "Requirement already satisfied: botocore<2.0.0,>=1.29.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents) (1.38.38)\n",
      "Requirement already satisfied: docstring-parser<1.0,>=0.15 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents) (0.16)\n",
      "Requirement already satisfied: mcp<2.0.0,>=1.8.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents) (1.11.0)\n",
      "Requirement already satisfied: opentelemetry-api<2.0.0,>=1.30.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents) (1.35.0)\n",
      "Requirement already satisfied: opentelemetry-sdk<2.0.0,>=1.30.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents) (1.35.0)\n",
      "Requirement already satisfied: pydantic<3.0.0,>=2.0.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents) (2.9.2)\n",
      "Requirement already satisfied: typing-extensions<5.0.0,>=4.13.2 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents) (4.14.0)\n",
      "Requirement already satisfied: watchdog<7.0.0,>=6.0.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents) (6.0.0)\n",
      "Requirement already satisfied: jmespath<2.0.0,>=0.7.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from boto3<2.0.0,>=1.26.0->strands-agents) (1.0.1)\n",
      "Requirement already satisfied: s3transfer<0.14.0,>=0.13.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from boto3<2.0.0,>=1.26.0->strands-agents) (0.13.0)\n",
      "Requirement already satisfied: python-dateutil<3.0.0,>=2.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from botocore<2.0.0,>=1.29.0->strands-agents) (2.9.0.post0)\n",
      "Requirement already satisfied: urllib3!=2.2.0,<3,>=1.25.4 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from botocore<2.0.0,>=1.29.0->strands-agents) (2.4.0)\n",
      "Requirement already satisfied: anyio>=4.5 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from mcp<2.0.0,>=1.8.0->strands-agents) (4.9.0)\n",
      "Requirement already satisfied: httpx-sse>=0.4 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from mcp<2.0.0,>=1.8.0->strands-agents) (0.4.1)\n",
      "Requirement already satisfied: httpx>=0.27 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from mcp<2.0.0,>=1.8.0->strands-agents) (0.28.1)\n",
      "Requirement already satisfied: jsonschema>=4.20.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from mcp<2.0.0,>=1.8.0->strands-agents) (4.24.0)\n",
      "Requirement already satisfied: pydantic-settings>=2.5.2 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from mcp<2.0.0,>=1.8.0->strands-agents) (2.10.1)\n",
      "Requirement already satisfied: python-multipart>=0.0.9 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from mcp<2.0.0,>=1.8.0->strands-agents) (0.0.20)\n",
      "Requirement already satisfied: sse-starlette>=1.6.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from mcp<2.0.0,>=1.8.0->strands-agents) (2.4.1)\n",
      "Requirement already satisfied: starlette>=0.27 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from mcp<2.0.0,>=1.8.0->strands-agents) (0.46.2)\n",
      "Requirement already satisfied: uvicorn>=0.23.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from mcp<2.0.0,>=1.8.0->strands-agents) (0.34.3)\n",
      "Requirement already satisfied: importlib-metadata<8.8.0,>=6.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from opentelemetry-api<2.0.0,>=1.30.0->strands-agents) (6.11.0)\n",
      "Requirement already satisfied: zipp>=0.5 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from importlib-metadata<8.8.0,>=6.0->opentelemetry-api<2.0.0,>=1.30.0->strands-agents) (3.23.0)\n",
      "Requirement already satisfied: opentelemetry-semantic-conventions==0.56b0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from opentelemetry-sdk<2.0.0,>=1.30.0->strands-agents) (0.56b0)\n",
      "Requirement already satisfied: annotated-types>=0.6.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from pydantic<3.0.0,>=2.0.0->strands-agents) (0.7.0)\n",
      "Requirement already satisfied: pydantic-core==2.23.4 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from pydantic<3.0.0,>=2.0.0->strands-agents) (2.23.4)\n",
      "Requirement already satisfied: six>=1.5 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from python-dateutil<3.0.0,>=2.1->botocore<2.0.0,>=1.29.0->strands-agents) (1.17.0)\n",
      "Requirement already satisfied: exceptiongroup>=1.0.2 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from anyio>=4.5->mcp<2.0.0,>=1.8.0->strands-agents) (1.3.0)\n",
      "Requirement already satisfied: idna>=2.8 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from anyio>=4.5->mcp<2.0.0,>=1.8.0->strands-agents) (3.10)\n",
      "Requirement already satisfied: sniffio>=1.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from anyio>=4.5->mcp<2.0.0,>=1.8.0->strands-agents) (1.3.1)\n",
      "Requirement already satisfied: certifi in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from httpx>=0.27->mcp<2.0.0,>=1.8.0->strands-agents) (2025.6.15)\n",
      "Requirement already satisfied: httpcore==1.* in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from httpx>=0.27->mcp<2.0.0,>=1.8.0->strands-agents) (1.0.9)\n",
      "Requirement already satisfied: h11>=0.16 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from httpcore==1.*->httpx>=0.27->mcp<2.0.0,>=1.8.0->strands-agents) (0.16.0)\n",
      "Requirement already satisfied: attrs>=22.2.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from jsonschema>=4.20.0->mcp<2.0.0,>=1.8.0->strands-agents) (25.3.0)\n",
      "Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from jsonschema>=4.20.0->mcp<2.0.0,>=1.8.0->strands-agents) (2025.4.1)\n",
      "Requirement already satisfied: referencing>=0.28.4 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from jsonschema>=4.20.0->mcp<2.0.0,>=1.8.0->strands-agents) (0.36.2)\n",
      "Requirement already satisfied: rpds-py>=0.7.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from jsonschema>=4.20.0->mcp<2.0.0,>=1.8.0->strands-agents) (0.25.1)\n",
      "Requirement already satisfied: python-dotenv>=0.21.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from pydantic-settings>=2.5.2->mcp<2.0.0,>=1.8.0->strands-agents) (1.1.1)\n",
      "Requirement already satisfied: typing-inspection>=0.4.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from pydantic-settings>=2.5.2->mcp<2.0.0,>=1.8.0->strands-agents) (0.4.1)\n",
      "Requirement already satisfied: click>=7.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from uvicorn>=0.23.1->mcp<2.0.0,>=1.8.0->strands-agents) (8.1.8)\n",
      "Requirement already satisfied: strands-agents-tools in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (0.1.8)\n",
      "Requirement already satisfied: aws-requests-auth<0.5.0,>=0.4.3 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents-tools) (0.4.3)\n",
      "Requirement already satisfied: colorama<0.5.0,>=0.4.6 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents-tools) (0.4.6)\n",
      "Requirement already satisfied: dill<0.5.0,>=0.4.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents-tools) (0.4.0)\n",
      "Requirement already satisfied: pillow<12.0.0,>=11.2.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents-tools) (11.2.1)\n",
      "Requirement already satisfied: prompt-toolkit<4.0.0,>=3.0.51 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents-tools) (3.0.51)\n",
      "Requirement already satisfied: pyjwt<3.0.0,>=2.10.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents-tools) (2.10.1)\n",
      "Requirement already satisfied: rich<15.0.0,>=14.0.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents-tools) (14.0.0)\n",
      "Requirement already satisfied: slack-bolt<2.0.0,>=1.23.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents-tools) (1.23.0)\n",
      "Requirement already satisfied: strands-agents<1.0.0,>=0.1.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents-tools) (0.2.1)\n",
      "Requirement already satisfied: sympy<2.0.0,>=1.12.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents-tools) (1.14.0)\n",
      "Requirement already satisfied: tenacity<10.0.0,>=9.1.2 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents-tools) (9.1.2)\n",
      "Requirement already satisfied: watchdog<7.0.0,>=6.0.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents-tools) (6.0.0)\n",
      "Requirement already satisfied: requests>=0.14.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from aws-requests-auth<0.5.0,>=0.4.3->strands-agents-tools) (2.32.4)\n",
      "Requirement already satisfied: wcwidth in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from prompt-toolkit<4.0.0,>=3.0.51->strands-agents-tools) (0.2.13)\n",
      "Requirement already satisfied: markdown-it-py>=2.2.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from rich<15.0.0,>=14.0.0->strands-agents-tools) (3.0.0)\n",
      "Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from rich<15.0.0,>=14.0.0->strands-agents-tools) (2.19.1)\n",
      "Requirement already satisfied: typing-extensions<5.0,>=4.0.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from rich<15.0.0,>=14.0.0->strands-agents-tools) (4.14.0)\n",
      "Requirement already satisfied: slack_sdk<4,>=3.35.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from slack-bolt<2.0.0,>=1.23.0->strands-agents-tools) (3.36.0)\n",
      "Requirement already satisfied: boto3<2.0.0,>=1.26.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (1.38.38)\n",
      "Requirement already satisfied: botocore<2.0.0,>=1.29.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (1.38.38)\n",
      "Requirement already satisfied: docstring-parser<1.0,>=0.15 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (0.16)\n",
      "Requirement already satisfied: mcp<2.0.0,>=1.8.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (1.11.0)\n",
      "Requirement already satisfied: opentelemetry-api<2.0.0,>=1.30.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (1.35.0)\n",
      "Requirement already satisfied: opentelemetry-sdk<2.0.0,>=1.30.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (1.35.0)\n",
      "Requirement already satisfied: pydantic<3.0.0,>=2.0.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (2.9.2)\n",
      "Requirement already satisfied: jmespath<2.0.0,>=0.7.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from boto3<2.0.0,>=1.26.0->strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (1.0.1)\n",
      "Requirement already satisfied: s3transfer<0.14.0,>=0.13.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from boto3<2.0.0,>=1.26.0->strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (0.13.0)\n",
      "Requirement already satisfied: python-dateutil<3.0.0,>=2.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from botocore<2.0.0,>=1.29.0->strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (2.9.0.post0)\n",
      "Requirement already satisfied: urllib3!=2.2.0,<3,>=1.25.4 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from botocore<2.0.0,>=1.29.0->strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (2.4.0)\n",
      "Requirement already satisfied: anyio>=4.5 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from mcp<2.0.0,>=1.8.0->strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (4.9.0)\n",
      "Requirement already satisfied: httpx-sse>=0.4 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from mcp<2.0.0,>=1.8.0->strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (0.4.1)\n",
      "Requirement already satisfied: httpx>=0.27 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from mcp<2.0.0,>=1.8.0->strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (0.28.1)\n",
      "Requirement already satisfied: jsonschema>=4.20.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from mcp<2.0.0,>=1.8.0->strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (4.24.0)\n",
      "Requirement already satisfied: pydantic-settings>=2.5.2 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from mcp<2.0.0,>=1.8.0->strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (2.10.1)\n",
      "Requirement already satisfied: python-multipart>=0.0.9 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from mcp<2.0.0,>=1.8.0->strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (0.0.20)\n",
      "Requirement already satisfied: sse-starlette>=1.6.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from mcp<2.0.0,>=1.8.0->strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (2.4.1)\n",
      "Requirement already satisfied: starlette>=0.27 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from mcp<2.0.0,>=1.8.0->strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (0.46.2)\n",
      "Requirement already satisfied: uvicorn>=0.23.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from mcp<2.0.0,>=1.8.0->strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (0.34.3)\n",
      "Requirement already satisfied: importlib-metadata<8.8.0,>=6.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from opentelemetry-api<2.0.0,>=1.30.0->strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (6.11.0)\n",
      "Requirement already satisfied: zipp>=0.5 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from importlib-metadata<8.8.0,>=6.0->opentelemetry-api<2.0.0,>=1.30.0->strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (3.23.0)\n",
      "Requirement already satisfied: opentelemetry-semantic-conventions==0.56b0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from opentelemetry-sdk<2.0.0,>=1.30.0->strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (0.56b0)\n",
      "Requirement already satisfied: annotated-types>=0.6.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from pydantic<3.0.0,>=2.0.0->strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (0.7.0)\n",
      "Requirement already satisfied: pydantic-core==2.23.4 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from pydantic<3.0.0,>=2.0.0->strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (2.23.4)\n",
      "Requirement already satisfied: six>=1.5 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from python-dateutil<3.0.0,>=2.1->botocore<2.0.0,>=1.29.0->strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (1.17.0)\n",
      "Requirement already satisfied: mpmath<1.4,>=1.1.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from sympy<2.0.0,>=1.12.0->strands-agents-tools) (1.3.0)\n",
      "Requirement already satisfied: exceptiongroup>=1.0.2 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from anyio>=4.5->mcp<2.0.0,>=1.8.0->strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (1.3.0)\n",
      "Requirement already satisfied: idna>=2.8 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from anyio>=4.5->mcp<2.0.0,>=1.8.0->strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (3.10)\n",
      "Requirement already satisfied: sniffio>=1.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from anyio>=4.5->mcp<2.0.0,>=1.8.0->strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (1.3.1)\n",
      "Requirement already satisfied: certifi in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from httpx>=0.27->mcp<2.0.0,>=1.8.0->strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (2025.6.15)\n",
      "Requirement already satisfied: httpcore==1.* in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from httpx>=0.27->mcp<2.0.0,>=1.8.0->strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (1.0.9)\n",
      "Requirement already satisfied: h11>=0.16 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from httpcore==1.*->httpx>=0.27->mcp<2.0.0,>=1.8.0->strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (0.16.0)\n",
      "Requirement already satisfied: attrs>=22.2.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from jsonschema>=4.20.0->mcp<2.0.0,>=1.8.0->strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (25.3.0)\n",
      "Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from jsonschema>=4.20.0->mcp<2.0.0,>=1.8.0->strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (2025.4.1)\n",
      "Requirement already satisfied: referencing>=0.28.4 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from jsonschema>=4.20.0->mcp<2.0.0,>=1.8.0->strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (0.36.2)\n",
      "Requirement already satisfied: rpds-py>=0.7.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from jsonschema>=4.20.0->mcp<2.0.0,>=1.8.0->strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (0.25.1)\n",
      "Requirement already satisfied: mdurl~=0.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from markdown-it-py>=2.2.0->rich<15.0.0,>=14.0.0->strands-agents-tools) (0.1.2)\n",
      "Requirement already satisfied: python-dotenv>=0.21.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from pydantic-settings>=2.5.2->mcp<2.0.0,>=1.8.0->strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (1.1.1)\n",
      "Requirement already satisfied: typing-inspection>=0.4.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from pydantic-settings>=2.5.2->mcp<2.0.0,>=1.8.0->strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (0.4.1)\n",
      "Requirement already satisfied: charset_normalizer<4,>=2 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from requests>=0.14.0->aws-requests-auth<0.5.0,>=0.4.3->strands-agents-tools) (3.4.2)\n",
      "Requirement already satisfied: click>=7.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from uvicorn>=0.23.1->mcp<2.0.0,>=1.8.0->strands-agents<1.0.0,>=0.1.0->strands-agents-tools) (8.1.8)\n",
      "Requirement already satisfied: strands-agents[openai] in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (0.2.1)\n",
      "Requirement already satisfied: boto3<2.0.0,>=1.26.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents[openai]) (1.38.38)\n",
      "Requirement already satisfied: botocore<2.0.0,>=1.29.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents[openai]) (1.38.38)\n",
      "Requirement already satisfied: docstring-parser<1.0,>=0.15 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents[openai]) (0.16)\n",
      "Requirement already satisfied: mcp<2.0.0,>=1.8.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents[openai]) (1.11.0)\n",
      "Requirement already satisfied: opentelemetry-api<2.0.0,>=1.30.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents[openai]) (1.35.0)\n",
      "Requirement already satisfied: opentelemetry-sdk<2.0.0,>=1.30.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents[openai]) (1.35.0)\n",
      "Requirement already satisfied: pydantic<3.0.0,>=2.0.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents[openai]) (2.9.2)\n",
      "Requirement already satisfied: typing-extensions<5.0.0,>=4.13.2 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents[openai]) (4.14.0)\n",
      "Requirement already satisfied: watchdog<7.0.0,>=6.0.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents[openai]) (6.0.0)\n",
      "Requirement already satisfied: openai<2.0.0,>=1.68.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from strands-agents[openai]) (1.95.1)\n",
      "Requirement already satisfied: jmespath<2.0.0,>=0.7.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from boto3<2.0.0,>=1.26.0->strands-agents[openai]) (1.0.1)\n",
      "Requirement already satisfied: s3transfer<0.14.0,>=0.13.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from boto3<2.0.0,>=1.26.0->strands-agents[openai]) (0.13.0)\n",
      "Requirement already satisfied: python-dateutil<3.0.0,>=2.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from botocore<2.0.0,>=1.29.0->strands-agents[openai]) (2.9.0.post0)\n",
      "Requirement already satisfied: urllib3!=2.2.0,<3,>=1.25.4 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from botocore<2.0.0,>=1.29.0->strands-agents[openai]) (2.4.0)\n",
      "Requirement already satisfied: anyio>=4.5 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from mcp<2.0.0,>=1.8.0->strands-agents[openai]) (4.9.0)\n",
      "Requirement already satisfied: httpx-sse>=0.4 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from mcp<2.0.0,>=1.8.0->strands-agents[openai]) (0.4.1)\n",
      "Requirement already satisfied: httpx>=0.27 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from mcp<2.0.0,>=1.8.0->strands-agents[openai]) (0.28.1)\n",
      "Requirement already satisfied: jsonschema>=4.20.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from mcp<2.0.0,>=1.8.0->strands-agents[openai]) (4.24.0)\n",
      "Requirement already satisfied: pydantic-settings>=2.5.2 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from mcp<2.0.0,>=1.8.0->strands-agents[openai]) (2.10.1)\n",
      "Requirement already satisfied: python-multipart>=0.0.9 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from mcp<2.0.0,>=1.8.0->strands-agents[openai]) (0.0.20)\n",
      "Requirement already satisfied: sse-starlette>=1.6.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from mcp<2.0.0,>=1.8.0->strands-agents[openai]) (2.4.1)\n",
      "Requirement already satisfied: starlette>=0.27 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from mcp<2.0.0,>=1.8.0->strands-agents[openai]) (0.46.2)\n",
      "Requirement already satisfied: uvicorn>=0.23.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from mcp<2.0.0,>=1.8.0->strands-agents[openai]) (0.34.3)\n",
      "Requirement already satisfied: distro<2,>=1.7.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from openai<2.0.0,>=1.68.0->strands-agents[openai]) (1.9.0)\n",
      "Requirement already satisfied: jiter<1,>=0.4.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from openai<2.0.0,>=1.68.0->strands-agents[openai]) (0.10.0)\n",
      "Requirement already satisfied: sniffio in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from openai<2.0.0,>=1.68.0->strands-agents[openai]) (1.3.1)\n",
      "Requirement already satisfied: tqdm>4 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from openai<2.0.0,>=1.68.0->strands-agents[openai]) (4.67.1)\n",
      "Requirement already satisfied: exceptiongroup>=1.0.2 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from anyio>=4.5->mcp<2.0.0,>=1.8.0->strands-agents[openai]) (1.3.0)\n",
      "Requirement already satisfied: idna>=2.8 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from anyio>=4.5->mcp<2.0.0,>=1.8.0->strands-agents[openai]) (3.10)\n",
      "Requirement already satisfied: certifi in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from httpx>=0.27->mcp<2.0.0,>=1.8.0->strands-agents[openai]) (2025.6.15)\n",
      "Requirement already satisfied: httpcore==1.* in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from httpx>=0.27->mcp<2.0.0,>=1.8.0->strands-agents[openai]) (1.0.9)\n",
      "Requirement already satisfied: h11>=0.16 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from httpcore==1.*->httpx>=0.27->mcp<2.0.0,>=1.8.0->strands-agents[openai]) (0.16.0)\n",
      "Requirement already satisfied: importlib-metadata<8.8.0,>=6.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from opentelemetry-api<2.0.0,>=1.30.0->strands-agents[openai]) (6.11.0)\n",
      "Requirement already satisfied: zipp>=0.5 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from importlib-metadata<8.8.0,>=6.0->opentelemetry-api<2.0.0,>=1.30.0->strands-agents[openai]) (3.23.0)\n",
      "Requirement already satisfied: opentelemetry-semantic-conventions==0.56b0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from opentelemetry-sdk<2.0.0,>=1.30.0->strands-agents[openai]) (0.56b0)\n",
      "Requirement already satisfied: annotated-types>=0.6.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from pydantic<3.0.0,>=2.0.0->strands-agents[openai]) (0.7.0)\n",
      "Requirement already satisfied: pydantic-core==2.23.4 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from pydantic<3.0.0,>=2.0.0->strands-agents[openai]) (2.23.4)\n",
      "Requirement already satisfied: six>=1.5 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from python-dateutil<3.0.0,>=2.1->botocore<2.0.0,>=1.29.0->strands-agents[openai]) (1.17.0)\n",
      "Requirement already satisfied: attrs>=22.2.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from jsonschema>=4.20.0->mcp<2.0.0,>=1.8.0->strands-agents[openai]) (25.3.0)\n",
      "Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from jsonschema>=4.20.0->mcp<2.0.0,>=1.8.0->strands-agents[openai]) (2025.4.1)\n",
      "Requirement already satisfied: referencing>=0.28.4 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from jsonschema>=4.20.0->mcp<2.0.0,>=1.8.0->strands-agents[openai]) (0.36.2)\n",
      "Requirement already satisfied: rpds-py>=0.7.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from jsonschema>=4.20.0->mcp<2.0.0,>=1.8.0->strands-agents[openai]) (0.25.1)\n",
      "Requirement already satisfied: python-dotenv>=0.21.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from pydantic-settings>=2.5.2->mcp<2.0.0,>=1.8.0->strands-agents[openai]) (1.1.1)\n",
      "Requirement already satisfied: typing-inspection>=0.4.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from pydantic-settings>=2.5.2->mcp<2.0.0,>=1.8.0->strands-agents[openai]) (0.4.1)\n",
      "Requirement already satisfied: click>=7.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from uvicorn>=0.23.1->mcp<2.0.0,>=1.8.0->strands-agents[openai]) (8.1.8)\n"
     ]
    }
   ],
   "source": [
    "#!pip install -r requirements.txt\n",
    "!pip install strands-agents\n",
    "!pip install strands-agents-tools\n",
    "!pip install strands-agents[openai]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "from strands import Agent, tool\n",
    "from strands_tools import file_write\n",
    "from strands.models.openai import OpenAIModel"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在这个模块中，我们将创建一个基于 orchestrator 的多代理工作流程。\n",
    "\n",
    "\n",
    "<div style=\"text-align:left\">\n",
    "    <img src=\"images/architecture.png\" width=\"75%\" />\n",
    "</div>\n",
    "\n",
    "我们还将探索 `use_llm`，它允许我们创建嵌套的 agents。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Research Agent"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "让我们首先创建一个带有 http_request 工具的基础研究助手。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "RESEARCH_ASSISTANT_PROMPT = \"\"\"你是一个专业的研究助手。只专注于针对研究问题提供基于事实、有充分来源的信息。在可能的情况下始终引用你的信息来源。\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = OpenAIModel(\n",
    "    client_args={\n",
    "        \"api_key\": ##\"此处填写硅基流动key\",\n",
    "         \"base_url\": \"https://api.siliconflow.cn\"\n",
    "    },\n",
    "    # **model_config\n",
    "    model_id=\"deepseek-ai/DeepSeek-V3\",\n",
    "    params={\n",
    "        \"max_tokens\": 10000,\n",
    "        \"temperature\": 0.7,\n",
    "    }\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "亚马逊云科技（Amazon Web Services, AWS）是亚马逊公司（Amazon.com）旗下的云计算服务平台，于2006年正式推出。作为全球领先的云服务提供商，AWS提供包括计算、存储、数据库、网络、人工智能、机器学习、数据分析、安全等在内的200多种全功能云服务，服务范围覆盖全球多个区域和可用区。\n",
      "\n",
      "### **核心服务分类**\n",
      "1. **计算服务**  \n",
      "   - **Amazon EC2**（弹性计算云）：提供可扩展的虚拟服务器。  \n",
      "   - **AWS Lambda**：无服务器计算服务，支持事件驱动的代码执行。  \n",
      "\n",
      "2. **存储服务**  \n",
      "   - **Amazon S3**（简单存储服务）：对象存储服务，适用于大数据和备份。  \n",
      "   - **Amazon EBS**（弹性块存储）：为EC2提供块级存储。  \n",
      "\n",
      "3. **数据库服务**  \n",
      "   - **Amazon RDS**：托管的关系型数据库服务（支持MySQL、PostgreSQL等）。  \n",
      "   - **Amazon DynamoDB**：全托管的NoSQL数据库服务。  \n",
      "\n",
      "4. **网络服务**  \n",
      "   - **Amazon VPC**（虚拟私有云）：用于隔离云资源。  \n",
      "   - **AWS Direct Connect**：提供专线连接企业数据中心和AWS。  \n",
      "\n",
      "5. **人工智能与机器学习**  \n",
      "   - **Amazon SageMaker**：用于构建、训练和部署机器学习模型。  \n",
      "   - **Amazon Rekognition**：图像和视频分析服务。  \n",
      "\n",
      "6. **安全与合规**  \n",
      "   - **AWS IAM**（身份和访问管理）：管理用户权限。  \n",
      "   - **AWS Shield**：提供DDoS防护。  \n",
      "\n",
      "7. **数据分析**  \n",
      "   - **Amazon Redshift**：数据仓库解决方案。  \n",
      "   - **AWS Glue**：ETL（提取、转换、加载）服务。  \n",
      "\n",
      "### **市场地位**\n",
      "根据**Synergy Research Group**（2023年数据），AWS在全球云计算市场占据约**34%**的份额，领先于微软Azure（约22%）和谷歌云（约11%）[1]。  \n",
      "\n",
      "### **全球基础设施**\n",
      "AWS在全球拥有**30多个地理区域（Regions）**和**100多个可用区（Availability Zones, AZs）**，并计划进一步扩展[2]。其基础设施的高可用性和低延迟设计使其成为企业级应用的首选。  \n",
      "\n",
      "### **客户案例**\n",
      "AWS的客户包括**Netflix、NASA、Airbnb、Siemens**等各行业领军企业，广泛应用于流媒体、金融、医疗和物联网等领域。  \n",
      "\n",
      "### **参考来源**\n",
      "[1] Synergy Research Group. (2023). *Cloud Market Share Report*.  \n",
      "[2] AWS Official Website. (2024). *Global Infrastructure*.  \n",
      "\n",
      "如需更详细的技术文档或案例分析，可访问AWS官方网站（aws.amazon.com）或相关白皮书。"
     ]
    }
   ],
   "source": [
    "research_agent = Agent(\n",
    "    system_prompt=RESEARCH_ASSISTANT_PROMPT,\n",
    "    model=model\n",
    "    # tools=[http_request]  # Here you can enable an agentic ai search tool\n",
    ")\n",
    "\n",
    "query = \"亚马逊云科技的概述\"\n",
    "# Call the agent and return its response\n",
    "response = research_agent(query)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "现在我们可以将这个 agent 包装为一个工具，允许其他 agents 与之交互。\n",
    "\n",
    "#### 使用 Agent as Tools 的最佳实践\n",
    "\n",
    "实现\"Agents as Tools\"模式时,请注意：\n",
    "\n",
    "1. 清晰的工具文档：编写描述性的文档字符串，解释 agent 的专业知识\n",
    "2. 聚焦的系统提示：保持每个专业 agent 严格关注其领域\n",
    "3. 适当的响应处理：使用一致的模式提取和格式化响应\n",
    "4. 工具选择指导：为 orchestrator 提供明确的标准，说明何时使用每个专业 agent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "@tool\n",
    "def research_assistant(query: str) -> str:\n",
    "    \"\"\"\n",
    "    Process and respond to research-related queries.\n",
    "\n",
    "    Args:\n",
    "        query: A research question requiring factual information\n",
    "\n",
    "    Returns:\n",
    "        A detailed research answer with citations\n",
    "    \"\"\"\n",
    "    try:\n",
    "        # Strands agents makes it easy to create a specialized agent\n",
    "        research_agent = Agent(\n",
    "            system_prompt=RESEARCH_ASSISTANT_PROMPT,\n",
    "            model=model\n",
    "        )\n",
    "\n",
    "        # Call the agent and return its response\n",
    "        response = research_agent(query)\n",
    "        return str(response)\n",
    "    except Exception as e:\n",
    "        return f\"Error in research assistant: {str(e)}\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "现在让我们遵循最佳实践创建 `product_recommendation_assistant`、`trip_planning_assistant` 和 `orchestrator` agent。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 产品推荐助手"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "@tool\n",
    "def product_recommendation_assistant(query: str) -> str:\n",
    "    \"\"\"\n",
    "    Handle product recommendation queries by suggesting appropriate products.\n",
    "\n",
    "    Args:\n",
    "        query: A product inquiry with user preferences\n",
    "\n",
    "    Returns:\n",
    "        Personalized product recommendations with reasoning\n",
    "    \"\"\"\n",
    "    try:\n",
    "        product_agent = Agent(\n",
    "            system_prompt=\"\"\"你是一个专业的产品推荐助手。\n",
    "根据用户偏好提供个性化产品建议。始终引用你的信息来源。\"\"\",\n",
    "            model=model\n",
    "        )\n",
    "        # Call the agent and return its response\n",
    "        response = product_agent(query)\n",
    "\n",
    "        return str(response)\n",
    "    except Exception as e:\n",
    "        return f\"Error in product recommendation: {str(e)}\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "以下是一些值得考虑的电动汽车推荐，基于2023年市场数据（来源：CleanTechnica、Consumer Reports、EV Database）：\n",
      "\n",
      "### 1. **高端性能车型**\n",
      "- **Tesla Model S Plaid**  \n",
      "  - 续航：637公里（EPA）  \n",
      "  - 0-100km/h：2.1秒  \n",
      "  - 亮点：三电机、17英寸触控屏、尖端自动驾驶硬件  \n",
      "  - 参考价：约$110,000  \n",
      "\n",
      "- **Lucid Air Grand Touring**  \n",
      "  - 续航：830公里（EPA）  \n",
      "  - 0-100km/h：3秒  \n",
      "  - 亮点：豪华内饰、超快充电（300kW）  \n",
      "  - 参考价：约$138,000  \n",
      "\n",
      "### 2. **家用主流车型**\n",
      "- **Tesla Model Y**（2023年全球最畅销电动车）  \n",
      "  - 续航：530公里（长续航版）  \n",
      "  - 亮点：超大储物空间、OTA更新、超级充电网络  \n",
      "  - 参考价：约$50,000  \n",
      "\n",
      "- **Ford Mustang Mach-E**  \n",
      "  - 续航：490公里（加州路由版）  \n",
      "  - 亮点：运动调校、Sync 4A系统、性价比高  \n",
      "  - 参考价：约$47,000  \n",
      "\n",
      "### 3. **高性价比车型**\n",
      "- **Chevrolet Bolt EV/EUV**（2023年停产，但仍有库存优惠）  \n",
      "  - 续航：416公里  \n",
      "  - 亮点：最便宜的电动车之一，支持DC快充  \n",
      "  - 参考价：约$26,000（含补贴后）  \n",
      "\n",
      "- **Volkswagen ID.4**  \n",
      "  - 续航：450公里（Pro版）  \n",
      "  - 亮点：欧洲风格设计、免费充电（Electrify America网络）  \n",
      "  - 参考价：约$38,000  \n",
      "\n",
      "### 4. **中国品牌（全球市场）**\n",
      "- **BYD Atto 3**（海外版元PLUS）  \n",
      "  - 续航：420公里（WLTP）  \n",
      "  - 亮点：刀片电池、旋转中控屏  \n",
      "  - 参考价：约$35,000（亚太/欧洲）  \n",
      "\n",
      "- **MG ZS EV**  \n",
      "  - 续航：440公里（长续航版）  \n",
      "  - 亮点：性价比高，支持V2L（车辆对外放电）  \n",
      "  - 参考价：约$30,000  \n",
      "\n",
      "### 选购建议：\n",
      "- **关注充电网络**：特斯拉超充覆盖最广，其他品牌依赖第三方（如Electrify America）。  \n",
      "- **补贴政策**：美国联邦税收优惠最高$7,500（需符合条件），中国/欧洲各地政策不同。  \n",
      "- **试驾重点**：体验单踏板驾驶、车机流畅度、后排空间（电动车平台差异大）。  \n",
      "\n",
      "如需更精准推荐，请提供：  \n",
      "1. 预算范围  \n",
      "2. 主要用途（通勤/家庭/长途）  \n",
      "3. 所在地区（充电设施/政策不同）  \n",
      "\n",
      "（数据更新至2023年Q4）"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'以下是一些值得考虑的电动汽车推荐，基于2023年市场数据（来源：CleanTechnica、Consumer Reports、EV Database）：\\n\\n### 1. **高端性能车型**\\n- **Tesla Model S Plaid**  \\n  - 续航：637公里（EPA）  \\n  - 0-100km/h：2.1秒  \\n  - 亮点：三电机、17英寸触控屏、尖端自动驾驶硬件  \\n  - 参考价：约$110,000  \\n\\n- **Lucid Air Grand Touring**  \\n  - 续航：830公里（EPA）  \\n  - 0-100km/h：3秒  \\n  - 亮点：豪华内饰、超快充电（300kW）  \\n  - 参考价：约$138,000  \\n\\n### 2. **家用主流车型**\\n- **Tesla Model Y**（2023年全球最畅销电动车）  \\n  - 续航：530公里（长续航版）  \\n  - 亮点：超大储物空间、OTA更新、超级充电网络  \\n  - 参考价：约$50,000  \\n\\n- **Ford Mustang Mach-E**  \\n  - 续航：490公里（加州路由版）  \\n  - 亮点：运动调校、Sync 4A系统、性价比高  \\n  - 参考价：约$47,000  \\n\\n### 3. **高性价比车型**\\n- **Chevrolet Bolt EV/EUV**（2023年停产，但仍有库存优惠）  \\n  - 续航：416公里  \\n  - 亮点：最便宜的电动车之一，支持DC快充  \\n  - 参考价：约$26,000（含补贴后）  \\n\\n- **Volkswagen ID.4**  \\n  - 续航：450公里（Pro版）  \\n  - 亮点：欧洲风格设计、免费充电（Electrify America网络）  \\n  - 参考价：约$38,000  \\n\\n### 4. **中国品牌（全球市场）**\\n- **BYD Atto 3**（海外版元PLUS）  \\n  - 续航：420公里（WLTP）  \\n  - 亮点：刀片电池、旋转中控屏  \\n  - 参考价：约$35,000（亚太/欧洲）  \\n\\n- **MG ZS EV**  \\n  - 续航：440公里（长续航版）  \\n  - 亮点：性价比高，支持V2L（车辆对外放电）  \\n  - 参考价：约$30,000  \\n\\n### 选购建议：\\n- **关注充电网络**：特斯拉超充覆盖最广，其他品牌依赖第三方（如Electrify America）。  \\n- **补贴政策**：美国联邦税收优惠最高$7,500（需符合条件），中国/欧洲各地政策不同。  \\n- **试驾重点**：体验单踏板驾驶、车机流畅度、后排空间（电动车平台差异大）。  \\n\\n如需更精准推荐，请提供：  \\n1. 预算范围  \\n2. 主要用途（通勤/家庭/长途）  \\n3. 所在地区（充电设施/政策不同）  \\n\\n（数据更新至2023年Q4）\\n'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "product_recommendation_assistant(\"关于电动汽车的产品推荐\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 旅行规划助手"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "@tool\n",
    "def trip_planning_assistant(query: str) -> str:\n",
    "    \"\"\"\n",
    "    Create travel itineraries and provide travel advice.\n",
    "\n",
    "    Args:\n",
    "        query: A travel planning request with destination and preferences\n",
    "\n",
    "    Returns:\n",
    "        A detailed travel itinerary or travel advice\n",
    "    \"\"\"\n",
    "    try:\n",
    "        travel_agent = Agent(\n",
    "            system_prompt=\"\"\"你是一个专业的旅行规划助手。根据用户偏好创建详细的旅行行程。\"\"\",\n",
    "            model=model,\n",
    "        )\n",
    "        # Call the agent and return its response\n",
    "        response = travel_agent(query)\n",
    "\n",
    "        return str(response)\n",
    "    except Exception as e:\n",
    "        return f\"Error in trip planning: {str(e)}\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Orchestrator（编排器） Agent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define orchestrator system prompt with clear tool selection guidance\n",
    "MAIN_SYSTEM_PROMPT = \"\"\"\n",
    "你是一个将查询路由到专门 agents 的助手：\n",
    "- 对于研究问题和事实信息 → 使用 research_assistant 工具\n",
    "- 对于产品推荐和购物建议 → 使用 product_recommendation_assistant 工具\n",
    "- 对于旅行规划和行程 → 使用 trip_planning_assistant 工具\n",
    "- 对于不需要专业知识的简单问题 → 直接回答\n",
    "\n",
    "始终根据用户的查询选择最合适的工具。\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Strands Agents allows easy integration of agent tools\n",
    "orchestrator = Agent(\n",
    "    system_prompt=MAIN_SYSTEM_PROMPT,\n",
    "    model=model,\n",
    "    tools=[\n",
    "        research_assistant,\n",
    "        product_recommendation_assistant,\n",
    "        trip_planning_assistant,\n",
    "        file_write,\n",
    "    ],\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Tool #1: product_recommendation_assistant\n",
      "根据您的需求，这里为您推荐几款适合徒步的优质靴子，涵盖不同场景和预算，并附上来源信息：\n",
      "\n",
      "### 一、专业级重装徒步靴（适合崎岖地形/负重）\n",
      "1. **Scarpa Zodiac Plus GTX**  \n",
      "   - 亮点：Vibram大底+全皮面+Gore-Tex防水，适合技术性山地徒步  \n",
      "   - 来源：《Outdoor Gearlab》2023年最佳登山靴评测  \n",
      "   - 价格区间：¥2000-2500\n",
      "\n",
      "2. **Lowa Renegade GTX Mid**  \n",
      "   - 亮点：德国工艺+耐磨PU中底，连续5年获《Backpacker》杂志编辑选择奖  \n",
      "   - 来源：Backpacker 2022年装备指南  \n",
      "\n",
      "### 二、中量级多功能靴（3季适用）\n",
      "1. **Salomon Quest 4 GTX**  \n",
      "   - 亮点：智能鞋带系统+Contagrip高抓地底，被《Gear Patrol》评为最佳全能靴  \n",
      "   - 实测数据：比前代减重15%（来源：Salomon实验室报告）  \n",
      "\n",
      "2. **Merrell Moab 3 Mid**  \n",
      "   - 亮点：性价比之王，Air Cushion缓震技术  \n",
      "   - 来源：美国REI销量冠军（2023年Q2数据）  \n",
      "\n",
      "### 三、轻量快速徒步鞋（适合轻装/成熟步道）\n",
      "1. **Altra Lone Peak ALL-WTHR**  \n",
      "   - 特色：宽楦设计+零落差中底，获《Trail Runner》2023年最佳徒步跑鞋  \n",
      "   - 适合：追求自然步态的用户  \n",
      "\n",
      "2. **HOKA Anacapa Mid GTX**  \n",
      "   - 亮点：超厚缓震中底+环保材料（50%再生材料）  \n",
      "   - 来源：HOKA 2023可持续发展报告  \n",
      "\n",
      "### 选购建议：\n",
      "1. **试穿要点**：建议下午脚部肿胀时试穿，预留1cm脚尖空间（来源：美国足病医学会建议）  \n",
      "2. **防水选择**：Gore-Tex适合多雨环境，但透气性较eVent略差（来源：《Outside》材质对比测试）  \n",
      "3. **保养提示**：皮质靴需定期用专用护理油（如Sno-Seal），可延长2-3倍使用寿命（来源：Scarpa保养手册）  \n",
      "\n",
      "近期折扣信息：  \n",
      "- 亚马逊Prime Day（7月11-12日）将有多款Salomon/Skechers徒步鞋5折  \n",
      "- 迪卡侬MH500系列正在进行季末清仓（来源：各品牌官网公告）  \n",
      "\n",
      "需要更具体的推荐（如女性专款/沙漠靴等），可告诉我您的详细需求。\n",
      "Tool #2: file_write\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #000080; text-decoration-color: #000080\">╔══════════ </span><span style=\"color: #000080; text-decoration-color: #000080; font-weight: bold\">File Write Operation</span><span style=\"color: #000080; text-decoration-color: #000080\"> ══════════╗</span>\n",
       "<span style=\"color: #000080; text-decoration-color: #000080\">║</span>                                          <span style=\"color: #000080; text-decoration-color: #000080\">║</span>\n",
       "<span style=\"color: #000080; text-decoration-color: #000080\">║</span> <span style=\"color: #008080; text-decoration-color: #008080\">Path: </span><span style=\"color: #808000; text-decoration-color: #808000\">./hiking_boots_recommendations.txt</span> <span style=\"color: #000080; text-decoration-color: #000080\">║</span>\n",
       "<span style=\"color: #000080; text-decoration-color: #000080\">║</span> <span style=\"color: #008080; text-decoration-color: #008080\">Size: </span><span style=\"color: #808000; text-decoration-color: #808000\">1087 characters</span>                    <span style=\"color: #000080; text-decoration-color: #000080\">║</span>\n",
       "<span style=\"color: #000080; text-decoration-color: #000080\">║</span>                                          <span style=\"color: #000080; text-decoration-color: #000080\">║</span>\n",
       "<span style=\"color: #000080; text-decoration-color: #000080\">╚══════════════════════════════════════════╝</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "\u001b[34m╔═\u001b[0m\u001b[34m═════════\u001b[0m\u001b[34m \u001b[0m\u001b[1;34mFile Write Operation\u001b[0m\u001b[34m \u001b[0m\u001b[34m═════════\u001b[0m\u001b[34m═╗\u001b[0m\n",
       "\u001b[34m║\u001b[0m                                          \u001b[34m║\u001b[0m\n",
       "\u001b[34m║\u001b[0m \u001b[36mPath: \u001b[0m\u001b[33m./hiking_boots_recommendations.txt\u001b[0m \u001b[34m║\u001b[0m\n",
       "\u001b[34m║\u001b[0m \u001b[36mSize: \u001b[0m\u001b[33m1087 characters\u001b[0m                    \u001b[34m║\u001b[0m\n",
       "\u001b[34m║\u001b[0m                                          \u001b[34m║\u001b[0m\n",
       "\u001b[34m╚══════════════════════════════════════════╝\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #008000; text-decoration-color: #008000\">╔═══════════════════════ </span><span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">Write Successful</span><span style=\"color: #008000; text-decoration-color: #008000\"> ════════════════════════╗</span>\n",
       "<span style=\"color: #008000; text-decoration-color: #008000\">║</span> <span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">File written successfully to ./hiking_boots_recommendations.txt</span> <span style=\"color: #008000; text-decoration-color: #008000\">║</span>\n",
       "<span style=\"color: #008000; text-decoration-color: #008000\">╚═════════════════════════════════════════════════════════════════╝</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "\u001b[32m╔═\u001b[0m\u001b[32m══════════════════════\u001b[0m\u001b[32m \u001b[0m\u001b[1;32mWrite Successful\u001b[0m\u001b[32m \u001b[0m\u001b[32m═══════════════════════\u001b[0m\u001b[32m═╗\u001b[0m\n",
       "\u001b[32m║\u001b[0m \u001b[1;32mFile written successfully to ./hiking_boots_recommendations.txt\u001b[0m \u001b[32m║\u001b[0m\n",
       "\u001b[32m╚═════════════════════════════════════════════════════════════════╝\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "徒步靴的推荐已成功写入当前目录下的 `hiking_boots_recommendations.txt` 文件中。您可以在文件中查看详细的推荐内容和选购建议。如果需要进一步的帮助或其他特定需求，请随时告诉我！"
     ]
    }
   ],
   "source": [
    "# Example: E-commerce Customer Service System\n",
    "customer_query = (\n",
    "    \"我在寻找徒步靴。将最终响应写入当前目录。\"\n",
    ")\n",
    "\n",
    "os.environ[\"BYPASS_TOOL_CONSENT\"] = \"true\"\n",
    "\n",
    "# The orchestrator automatically determines this requires multiple specialized agents\n",
    "response = orchestrator(customer_query)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "让我们查看 orchestrator 的消息。在这里你可以看到该 agent 决定使用子 agent 作为工具"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "orchestrator.messages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "customer_query = \"你能帮我规划我去成都的旅行吗？\"\n",
    "\n",
    "response = orchestrator(customer_query)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "orchestrator.messages"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 调用多个 agents"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "orchestrator.messages = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "query = \"你能对中国进行研究吗？同时帮我规划一个7天的旅行.\"\n",
    "\n",
    "orchestrator(query)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在幕后，orchestrator 将：\n",
    "1. 首先调用 research_assistant\n",
    "2. 然后调用 trip_planning_assistant\n",
    "3. 将这些专业响应组合成一个完整的答案，以解决这两个查询"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 顺序 Agent 通信模式"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "agent 工具还可以将多个 agents 组合在一起。在这个例子中，我们将 `research_agent` 的输出提供给 `summary_agent`，并返回汇总的响应。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "多个 agents 创建成功！\n",
      "\n",
      "🔍 研究 AGENT 正在处理: generative Ai\n",
      "\n",
      "以下是关于生成式人工智能（Generative AI）的综合信息，涵盖定义、核心技术、应用领域、挑战以及未来趋势，均基于权威来源和研究报告：\n",
      "\n",
      "---\n",
      "\n",
      "### **1. 定义与核心概念**\n",
      "生成式AI（Generative AI）指能够自主生成新内容（如文本、图像、音频、代码等）的人工智能系统。其核心是通过学习大量数据分布，模拟并创造类似但全新的输出。  \n",
      "- **与判别式AI的区别**：判别式模型（如分类器）用于区分数据类别，而生成式模型专注于创造数据（OpenAI, 2021）。\n",
      "\n",
      "---\n",
      "\n",
      "### **2. 关键技术**\n",
      "#### **(1) 生成对抗网络（GANs）**  \n",
      "- 由生成器（Generator）和判别器（Discriminator）组成，通过对抗训练生成逼真数据（Goodfellow et al., 2014）。  \n",
      "- **应用**：图像生成（如Deepfake）、艺术创作（如Artbreeder）。\n",
      "\n",
      "#### **(2) 变分自编码器（VAEs）**  \n",
      "- 通过编码-解码结构学习数据潜在分布，适合生成连续数据（如分子设计）（Kingma & Welling, 2013）。\n",
      "\n",
      "#### **(3) 自回归模型（如GPT系列）**  \n",
      "- 基于Transformer架构，通过预测序列中下一个token生成文本、代码等（OpenAI, 2020）。  \n",
      "- **示例**：ChatGPT（文本）、Codex（代码）。\n",
      "\n",
      "#### **(4) 扩散模型（Diffusion Models）**  \n",
      "- 通过逐步去噪过程生成高质量图像，如Stable Diffusion和DALL·E 2（Ho et al., 2020）。\n",
      "\n",
      "---\n",
      "\n",
      "### **3. 主要应用领域**\n",
      "#### **(1) 内容创作**  \n",
      "- **文本**：自动写作、对话系统（如ChatGPT）。  \n",
      "- **图像/视频**：AI绘画（MidJourney）、视频合成（Runway ML）。  \n",
      "- **音乐**：AIVA生成原创曲目。\n",
      "\n",
      "#### **(2) 医疗与科研**  \n",
      "- 药物分子设计（如Atomwise）、医学影像合成（ArXiv, 2022）。\n",
      "\n",
      "#### **(3) 工业与设计**  \n",
      "- 3D模型生成（NVIDIA Omniverse）、产品原型设计（Autodesk Generative Design）。\n",
      "\n",
      "#### **(4) 其他领域**  \n",
      "- **教育**：个性化学习材料生成。  \n",
      "- **金融**：合成数据训练风险模型（McKinsey, 2023）。\n",
      "\n",
      "---\n",
      "\n",
      "### **4. 挑战与风险**\n",
      "#### **(1) 伦理与滥用**  \n",
      "- 深度伪造（Deepfake）可能导致虚假信息传播（MIT Tech Review, 2023）。  \n",
      "- **版权争议**：生成内容是否侵犯训练数据版权（如Getty Images诉Stable Diffusion案）。\n",
      "\n",
      "#### **(2) 技术限制**  \n",
      "- **幻觉（Hallucination）**：生成不准确或虚构内容（如AI编造法律案例）。  \n",
      "- **数据偏见**：训练数据偏差导致输出歧视（如性别偏见）。\n",
      "\n",
      "#### **(3) 算力与成本**  \n",
      "- 训练大模型需巨额计算资源（GPT-3训练成本超1200万美元，据估算）。\n",
      "\n",
      "---\n",
      "\n",
      "### **5. 未来趋势**\n",
      "- **多模态生成**：同时处理文本、图像、音频（如Google Gemini）。  \n",
      "- **小型化与开源**：轻量级模型（如Meta的LLaMA）降低使用门槛。  \n",
      "- **监管框架**：欧盟《AI法案》等政策逐步规范生成式AI应用。\n",
      "\n",
      "---\n",
      "\n",
      "### **权威来源与进一步阅读**\n",
      "1. [OpenAI博客](https://openai.com/blog) - GPT系列技术细节。  \n",
      "2. [arXiv论文](https://arxiv.org/) - 搜索\"Generative AI\"获取最新研究。  \n",
      "3. Gartner报告（2023）：《生成式AI技术成熟度曲线》。  \n",
      "4. Stanford《AI Index Report》（2023） - 行业趋势与数据。  \n",
      "\n",
      "如需特定领域的深入分析（如医疗或法律影响），可提供更定向的文献或案例研究。以下是关于生成式人工智能（Generative AI）的综合信息，涵盖定义、核心技术、应用领域、挑战以及未来趋势，均基于权威来源和研究报告：\n",
      "\n",
      "---\n",
      "\n",
      "### **1. 定义与核心概念**\n",
      "生成式AI（Generative AI）指能够自主生成新内容（如文本、图像、音频、代码等）的人工智能系统。其核心是通过学习大量数据分布，模拟并创造类似但全新的输出。  \n",
      "- **与判别式AI的区别**：判别式模型（如分类器）用于区分数据类别，而生成式模型专注于创造数据（OpenAI, 2021）。\n",
      "\n",
      "---\n",
      "\n",
      "### **2. 关键技术**\n",
      "#### **(1) 生成对抗网络（GANs）**  \n",
      "- 由生成器（Generator）和判别器（Discriminator）组成，通过对抗训练生成逼真数据（Goodfellow et al., 2014）。  \n",
      "- **应用**：图像生成（如Deepfake）、艺术创作（如Artbreeder）。\n",
      "\n",
      "#### **(2) 变分自编码器（VAEs）**  \n",
      "- 通过编码-解码结构学习数据潜在分布，适合生成连续数据（如分子设计）（Kingma & Welling, 2013）。\n",
      "\n",
      "#### **(3) 自回归模型（如GPT系列）**  \n",
      "- 基于Transformer架构，通过预测序列中下一个token生成文本、代码等（OpenAI, 2020）。  \n",
      "- **示例**：ChatGPT（文本）、Codex（代码）。\n",
      "\n",
      "#### **(4) 扩散模型（Diffusion Models）**  \n",
      "- 通过逐步去噪过程生成高质量图像，如Stable Diffusion和DALL·E 2（Ho et al., 2020）。\n",
      "\n",
      "---\n",
      "\n",
      "### **3. 主要应用领域**\n",
      "#### **(1) 内容创作**  \n",
      "- **文本**：自动写作、对话系统（如ChatGPT）。  \n",
      "- **图像/视频**：AI绘画（MidJourney）、视频合成（Runway ML）。  \n",
      "- **音乐**：AIVA生成原创曲目。\n",
      "\n",
      "#### **(2) 医疗与科研**  \n",
      "- 药物分子设计（如Atomwise）、医学影像合成（ArXiv, 2022）。\n",
      "\n",
      "#### **(3) 工业与设计**  \n",
      "- 3D模型生成（NVIDIA Omniverse）、产品原型设计（Autodesk Generative Design）。\n",
      "\n",
      "#### **(4) 其他领域**  \n",
      "- **教育**：个性化学习材料生成。  \n",
      "- **金融**：合成数据训练风险模型（McKinsey, 2023）。\n",
      "\n",
      "---\n",
      "\n",
      "### **4. 挑战与风险**\n",
      "#### **(1) 伦理与滥用**  \n",
      "- 深度伪造（Deepfake）可能导致虚假信息传播（MIT Tech Review, 2023）。  \n",
      "- **版权争议**：生成内容是否侵犯训练数据版权（如Getty Images诉Stable Diffusion案）。\n",
      "\n",
      "#### **(2) 技术限制**  \n",
      "- **幻觉（Hallucination）**：生成不准确或虚构内容（如AI编造法律案例）。  \n",
      "- **数据偏见**：训练数据偏差导致输出歧视（如性别偏见）。\n",
      "\n",
      "#### **(3) 算力与成本**  \n",
      "- 训练大模型需巨额计算资源（GPT-3训练成本超1200万美元，据估算）。\n",
      "\n",
      "---\n",
      "\n",
      "### **5. 未来趋势**\n",
      "- **多模态生成**：同时处理文本、图像、音频（如Google Gemini）。  \n",
      "- **小型化与开源**：轻量级模型（如Meta的LLaMA）降低使用门槛。  \n",
      "- **监管框架**：欧盟《AI法案》等政策逐步规范生成式AI应用。\n",
      "\n",
      "---\n",
      "\n",
      "### **权威来源与进一步阅读**\n",
      "1. [OpenAI博客](https://openai.com/blog) - GPT系列技术细节。  \n",
      "2. [arXiv论文](https://arxiv.org/) - 搜索\"Generative AI\"获取最新研究。  \n",
      "3. Gartner报告（2023）：《生成式AI技术成熟度曲线》。  \n",
      "4. Stanford《AI Index Report》（2023） - 行业趋势与数据。  \n",
      "\n",
      "如需特定领域的深入分析（如医疗或法律影响），可提供更定向的文献或案例研究。\n",
      "\n",
      "{'role': 'assistant', 'content': [{'text': '以下是关于生成式人工智能（Generative AI）的综合信息，涵盖定义、核心技术、应用领域、挑战以及未来趋势，均基于权威来源和研究报告：\\n\\n---\\n\\n### **1. 定义与核心概念**\\n生成式AI（Generative AI）指能够自主生成新内容（如文本、图像、音频、代码等）的人工智能系统。其核心是通过学习大量数据分布，模拟并创造类似但全新的输出。  \\n- **与判别式AI的区别**：判别式模型（如分类器）用于区分数据类别，而生成式模型专注于创造数据（OpenAI, 2021）。\\n\\n---\\n\\n### **2. 关键技术**\\n#### **(1) 生成对抗网络（GANs）**  \\n- 由生成器（Generator）和判别器（Discriminator）组成，通过对抗训练生成逼真数据（Goodfellow et al., 2014）。  \\n- **应用**：图像生成（如Deepfake）、艺术创作（如Artbreeder）。\\n\\n#### **(2) 变分自编码器（VAEs）**  \\n- 通过编码-解码结构学习数据潜在分布，适合生成连续数据（如分子设计）（Kingma & Welling, 2013）。\\n\\n#### **(3) 自回归模型（如GPT系列）**  \\n- 基于Transformer架构，通过预测序列中下一个token生成文本、代码等（OpenAI, 2020）。  \\n- **示例**：ChatGPT（文本）、Codex（代码）。\\n\\n#### **(4) 扩散模型（Diffusion Models）**  \\n- 通过逐步去噪过程生成高质量图像，如Stable Diffusion和DALL·E 2（Ho et al., 2020）。\\n\\n---\\n\\n### **3. 主要应用领域**\\n#### **(1) 内容创作**  \\n- **文本**：自动写作、对话系统（如ChatGPT）。  \\n- **图像/视频**：AI绘画（MidJourney）、视频合成（Runway ML）。  \\n- **音乐**：AIVA生成原创曲目。\\n\\n#### **(2) 医疗与科研**  \\n- 药物分子设计（如Atomwise）、医学影像合成（ArXiv, 2022）。\\n\\n#### **(3) 工业与设计**  \\n- 3D模型生成（NVIDIA Omniverse）、产品原型设计（Autodesk Generative Design）。\\n\\n#### **(4) 其他领域**  \\n- **教育**：个性化学习材料生成。  \\n- **金融**：合成数据训练风险模型（McKinsey, 2023）。\\n\\n---\\n\\n### **4. 挑战与风险**\\n#### **(1) 伦理与滥用**  \\n- 深度伪造（Deepfake）可能导致虚假信息传播（MIT Tech Review, 2023）。  \\n- **版权争议**：生成内容是否侵犯训练数据版权（如Getty Images诉Stable Diffusion案）。\\n\\n#### **(2) 技术限制**  \\n- **幻觉（Hallucination）**：生成不准确或虚构内容（如AI编造法律案例）。  \\n- **数据偏见**：训练数据偏差导致输出歧视（如性别偏见）。\\n\\n#### **(3) 算力与成本**  \\n- 训练大模型需巨额计算资源（GPT-3训练成本超1200万美元，据估算）。\\n\\n---\\n\\n### **5. 未来趋势**\\n- **多模态生成**：同时处理文本、图像、音频（如Google Gemini）。  \\n- **小型化与开源**：轻量级模型（如Meta的LLaMA）降低使用门槛。  \\n- **监管框架**：欧盟《AI法案》等政策逐步规范生成式AI应用。\\n\\n---\\n\\n### **权威来源与进一步阅读**\\n1. [OpenAI博客](https://openai.com/blog) - GPT系列技术细节。  \\n2. [arXiv论文](https://arxiv.org/) - 搜索\"Generative AI\"获取最新研究。  \\n3. Gartner报告（2023）：《生成式AI技术成熟度曲线》。  \\n4. Stanford《AI Index Report》（2023） - 行业趋势与数据。  \\n\\n如需特定领域的深入分析（如医疗或法律影响），可提供更定向的文献或案例研究。'}]}\n",
      "---------------------\n",
      "\n",
      "✂️ 总结 AGENT 正在提炼研究内容\n",
      "\n",
      "**生成式人工智能（Generative AI）简明摘要**  \n",
      "\n",
      "**定义**：能自主生成文本、图像、音频等内容的人工智能，通过数据学习创造新输出，区别于判别式AI的分类功能。  \n",
      "\n",
      "**核心技术**：  \n",
      "1. **GANs**：对抗训练生成逼真数据（如Deepfake）。  \n",
      "2. **VAEs**：适用于连续数据生成（如分子设计）。  \n",
      "3. **自回归模型**（如GPT）：基于Transformer生成文本/代码。  \n",
      "4. **扩散模型**：逐步去噪生成高质量图像（如DALL·E 2）。  \n",
      "\n",
      "**应用领域**：  \n",
      "- **内容创作**：AI写作（ChatGPT）、绘画（MidJourney）、音乐生成。  \n",
      "- **医疗科研**：药物设计、医学影像合成。  \n",
      "- **工业设计**：3D建模、产品原型。  \n",
      "- **其他**：教育个性化材料、金融合成数据。  \n",
      "\n",
      "**挑战**：  \n",
      "- **伦理风险**：Deepfake虚假信息、版权争议。  \n",
      "- **技术缺陷**：输出不准确（幻觉）、数据偏见。  \n",
      "- **高成本**：如GPT-3训练耗资超1200万美元。  \n",
      "\n",
      "**未来趋势**：  \n",
      "- 多模态生成（如结合文本与图像）。  \n",
      "- 模型小型化与开源（如LLaMA）。  \n",
      "- 强化监管（如欧盟《AI法案》）。  \n",
      "\n",
      "**数据来源**：OpenAI、arXiv、Gartner等权威报告。  \n",
      "\n",
      "（字数：约250字，突出技术、应用、挑战与趋势的关键点）**生成式人工智能（Generative AI）简明摘要**  \n",
      "\n",
      "**定义**：能自主生成文本、图像、音频等内容的人工智能，通过数据学习创造新输出，区别于判别式AI的分类功能。  \n",
      "\n",
      "**核心技术**：  \n",
      "1. **GANs**：对抗训练生成逼真数据（如Deepfake）。  \n",
      "2. **VAEs**：适用于连续数据生成（如分子设计）。  \n",
      "3. **自回归模型**（如GPT）：基于Transformer生成文本/代码。  \n",
      "4. **扩散模型**：逐步去噪生成高质量图像（如DALL·E 2）。  \n",
      "\n",
      "**应用领域**：  \n",
      "- **内容创作**：AI写作（ChatGPT）、绘画（MidJourney）、音乐生成。  \n",
      "- **医疗科研**：药物设计、医学影像合成。  \n",
      "- **工业设计**：3D建模、产品原型。  \n",
      "- **其他**：教育个性化材料、金融合成数据。  \n",
      "\n",
      "**挑战**：  \n",
      "- **伦理风险**：Deepfake虚假信息、版权争议。  \n",
      "- **技术缺陷**：输出不准确（幻觉）、数据偏见。  \n",
      "- **高成本**：如GPT-3训练耗资超1200万美元。  \n",
      "\n",
      "**未来趋势**：  \n",
      "- 多模态生成（如结合文本与图像）。  \n",
      "- 模型小型化与开源（如LLaMA）。  \n",
      "- 强化监管（如欧盟《AI法案》）。  \n",
      "\n",
      "**数据来源**：OpenAI、arXiv、Gartner等权威报告。  \n",
      "\n",
      "（字数：约250字，突出技术、应用、挑战与趋势的关键点）\n"
     ]
    }
   ],
   "source": [
    "# 定义用户查询\n",
    "topic = \"generative Ai\"\n",
    "# 创建一个研究agent\n",
    "research_agent = Agent(\n",
    "    system_prompt=RESEARCH_ASSISTANT_PROMPT,\n",
    "    model=model\n",
    ")\n",
    "# 创建一个总结agent\n",
    "summary_agent = Agent(\n",
    "    system_prompt=\"\"\"\n",
    "    你是一位专注于将复杂信息提炼成清晰、简洁摘要的总结专家。\n",
    "    你的主要目标是提取详细信息中的关键点、主要论点和关键数据。\n",
    "    你应该在保持原始内容准确性的同时，使其更易于理解。\n",
    "    专注于清晰度、简洁性，并突出信息中最重要的方面。\n",
    "    \"\"\",\n",
    "    model=model\n",
    ")\n",
    "\n",
    "print(\"多个 agents 创建成功！\")\n",
    "print(f\"\\n🔍 研究 AGENT 正在处理: {topic}\\n\") \n",
    "try:\n",
    "    # Agent 1: 调用研究agent\n",
    "    research_response = research_agent(\n",
    "        f\"请收集关于{topic}的综合信息。\"\n",
    "    )\n",
    "    print(research_response)\n",
    "    print(research_response.message)\n",
    "    print(\"---------------------\")\n",
    "    research_text = research_response.message['content'][0][\"text\"]\n",
    "    print(\"\\n✂️ 总结 AGENT 正在提炼研究内容\\n\")\n",
    "    \n",
    "    # Agent 2: 请总结agent创建简洁摘要\n",
    "    summary_response = summary_agent(\n",
    "        f\"请为这项研究创建一个简洁的摘要: {research_text}\"\n",
    "    )\n",
    "    summary_text = summary_response.message['content'][0][\"text\"]\n",
    "    \n",
    "    print(summary_text)\n",
    "except Exception as e:\n",
    "    print(f\"研究助手出错: {str(e)}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 恭喜！\n",
    "\n",
    "你已经学会了如何在 Strands Agents 中将 agents 用作工具，以创建更复杂的代理应用程序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "conda_python3",
   "language": "python",
   "name": "conda_python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
